<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备状态" prop="deviceStatue">
        <el-select
          v-model="queryParams.deviceStatue"
          placeholder="设备状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in deviceStatueList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备编码" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        <el-select
          v-model="queryParams.customerId"
          placeholder="客户"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in customerList"
            :key="item.id"
            :label="item.customerName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleLock"
        >批量锁定</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUnLock"
        >批量解锁</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orderDevice:export']"
        >批量导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNum" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备编码" align="center" prop="deviceCode" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="设备状态" align="center" prop="deviceStatue">
        <template slot-scope="scope">
          <span v-if="scope.row.deviceStatue == 0">就绪</span>
          <span v-if="scope.row.deviceStatue == 1">在线</span>
          <span v-if="scope.row.deviceStatue == 2">离线</span>
          <span v-if="scope.row.deviceStatue == 3">待锁定</span>
          <span v-if="scope.row.deviceStatue == 4">锁定</span>
        </template>
      </el-table-column>
      <el-table-column label="锁定时间" align="center" prop="lockTime" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listOrderDevice,lockDeviceByIds,unLockDeviceByIds } from "@/api/system/orderDevice";
import { listCustomer} from "@/api/system/customer";
export default {
  name: "Device",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单关联设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        deviceId: null,
        deviceStatue: null,
        lockTime: null,
        gps: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNum: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备id不能为空", trigger: "blur" }
        ],
        deviceStatue: [
          { required: true, message: "设备状态;0:就绪；1：在线；2：离线；3：待锁定；4：锁定不能为空", trigger: "blur" }
        ],
      },
      customerList:[],
      deviceStatueList:[{id:'0',name:'就绪'},{id:'1',name:'在线'},{id:'2',name:'离线'},{id:'3',name:'待锁定'},{id:'4',name:'锁定'}]
    };
  },
  created() {
    this.getList();
    this.getCustomerList();
  },
  methods: {
    /** 查询客户列表 */
    getCustomerList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
      });
    },
    /** 查询订单关联设备列表 */
    getList() {
      this.loading = true;
      listOrderDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNum: null,
        deviceId: null,
        deviceStatue: null,
        lockTime: null,
        gps: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 批量锁定按钮操作 */
    handleLock (row) {
      let that = this;
      const ids = row.id || this.ids;
      this.$modal.confirm('是否锁定这些设备？').then(function() {
        lockDeviceByIds(ids).then(response => {
          let list = response.data;
          list.forEach(element => {
            if(element.code=="200"){
              setTimeout(() => {
                that.$notify({
                  title: '成功',
                  message: element.name+":"+element.msg,
                  type: 'success',
                  duration: 3000
                });
              }, 100);
              
            } else if(element.code=="300"){
              setTimeout(() => {
                that.$notify({
                  title: '告警',
                  message:element.name+":"+element.msg,
                  type: 'warning',
                  duration: 4000
                });
              }, 100);
            }else{
              setTimeout(() => {
                that.$notify.error({
                  title: '失败',
                  message:element.name+":"+element.msg,
                  duration: 5000
                });
              }, 100);
            }
          });
           that.getList();
           
        });
      }).catch(() => {});
    },
    /** 批量解锁按钮操作 */
    handleUnLock(row) {
      let that = this;
      const ids = row.id || this.ids;
      this.$modal.confirm('是否解锁这些设备？').then(function() {
        unLockDeviceByIds(ids).then(response => {
          let list = response.data;
          list.forEach(element => {
            if(element.code=="200"){
              setTimeout(() => {
                that.$notify({
                  title: '成功',
                  message: element.name+":"+element.msg,
                  type: 'success',
                  duration: 3000
                });
              }, 100);
              
            } else if(element.code=="300"){
              setTimeout(() => {
                that.$notify({
                  title: '告警',
                  message:element.name+":"+element.msg,
                  type: 'warning',
                  duration: 4000
                });
              }, 100);
            }else{
              setTimeout(() => {
                that.$notify.error({
                  title: '失败',
                  message:element.name+":"+element.msg,
                  duration: 5000
                });
              }, 100);
            }
          });
           that.getList();
           
        });

      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orderDevice/export', {
        ...this.queryParams
      }, `orderDevice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
